package com.dream.libxrec.view;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Size;

public class YUVGLSurfaceView extends GLSurfaceView {
    private YUVSurfaceViewRender mRender;
    private int mImageWidth = 0;
    private int mImageHeight = 0;

    private boolean isFixSize = false;


    public YUVGLSurfaceView(Context context) {
        super(context);
        initRender();
    }

    public YUVGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRender();
    }

    private void initRender() {
        setEGLContextClientVersion(2);//在setRenderer()方法前调用此方法
        mRender = new YUVSurfaceViewRender();
        setRenderer(mRender);
        setRenderMode(RENDERMODE_WHEN_DIRTY);
    }

    /**
     *
     * @param renderType 渲染的数据类型,YUVSurfaceViewRender
     *                   .RENDER_TYPE_NV21或YUVSurfaceViewRender.RENDER_TYPE_NV12
     * @param imageWidth YUV宽度
     * @param imageHeight YUV高度
     * @param isFixSize 是否适应宽高
     */
    public void init(int renderType,int imageWidth,
                     int imageHeight,
                     boolean isFixSize){
        mRender.init(renderType,imageWidth,imageHeight);
        this.isFixSize = isFixSize;
        this.mImageWidth = imageWidth;
        this.mImageHeight = imageHeight;

        if (isFixSize){
            //重新测量
            if (Looper.getMainLooper().isCurrentThread()){
                requestLayout();
            }else{
                //确保在UI线程执行
                new Handler(Looper.getMainLooper()).post(this::requestLayout);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        // 设置 View 的测量大小
        if (isFixSize){
            Size s = calculateViewSize(width, height,
                    mImageWidth,mImageHeight);
            setMeasuredDimension(s.getWidth(), s.getHeight());
        }else{
            setMeasuredDimension(width, height);
        }
    }

    public void onFrame(byte[] data){
        mRender.onFrame(data);
        requestRender();
    }

    /**
     * 为了确保页面不扭曲，适配摄像头分辨率获取合适的宽高
     *
     * @param imageWidth 摄像头分辨率宽
     * @param imageHeight 摄像头分辨率高
     * @return
     */
    public Size calculateViewSize(int viewWidth, int viewHeight,
                               int imageWidth, int imageHeight) {
        float viewRatio = (float) viewWidth / viewHeight;
        float cameraRatio = (float) imageWidth / imageHeight;

        if (viewRatio > cameraRatio) {
            // View 更宽，以高度为基准进行缩放
            int newWidth = (int) (viewHeight * cameraRatio);
            return new Size(newWidth, viewHeight);
        } else {
            // View 更高或比例相同，以宽度为基准进行缩放
            int newHeight = (int) (viewWidth / cameraRatio);
            return new Size(viewWidth, newHeight);
        }
    }
}